package client.comm.baoding.fragment

import android.content.Intent
import android.view.View
import androidx.core.view.ViewCompat
import androidx.lifecycle.lifecycleScope
import client.comm.baoding.R
import client.comm.baoding.adapter.SyPagingHeaderAdapter
import client.comm.baoding.adapter.TabTeamHeaderAdapter
import client.comm.baoding.adapter.Td2Adapter
import client.comm.baoding.api.bean.TdBean
import client.comm.baoding.api.bean.UserInfo
import client.comm.baoding.databinding.FragmentTabteam2Binding
import client.comm.baoding.ui.TdJlJlActivity
import client.comm.baoding.ui.vm.MainViewModel
import client.comm.commlib.base.BaseFragment
import client.comm.commlib.base.PagerFooterAdapter
import client.comm.commlib.network.data.PagingJsonResult
import client.comm.commlib.toast.toast
import client.comm.commlib.utils.StoreUtil
import client.comm.commlib.utils.Util
import client.comm.commlib.widget.EmptyLayout
import com.blankj.utilcode.util.LogUtils
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class TabTeamFragment : BaseFragment<FragmentTabteam2Binding, MainViewModel>() {

    override fun getViewId(): Int {
        return R.layout.fragment_tabteam2
    }

    override fun initViewModel(): MainViewModel = createViewModel()

    val adapter: Td2Adapter by lazy {
        Td2Adapter(requireContext(), mImgLoader).apply {
            refreshLoading = {
                if (itemCount == 0) {
                    binding.emptyLayout.errorType = EmptyLayout.NETWORK_LOADING
                }
            }
            refreshNotLoading = {
                binding.refreshLayout.isRefreshing = false
//                if (itemCount == 0) {
//                    binding.emptyLayout.errorType = EmptyLayout.NODATA
//                } else {
//                }
                binding.emptyLayout.visibility = View.GONE
            }
            refreshError = {
                if (itemCount == 0)
                    binding.emptyLayout.errorType = EmptyLayout.NETWORK_ERROR
            }
        }
    }
    val headerAdapter: TabTeamHeaderAdapter by lazy {
        TabTeamHeaderAdapter(mImgLoader, requireContext())
    }

    override fun initWidget() {
//        binding.statusBar.post {
//            val h = Util.getStatusBarHeight(requireContext())
//            val p = binding.statusBar.layoutParams
//            p.height = h
//            binding.statusBar.layoutParams = p
//            Util.setBg(resources!!.getColor(R.color.white), requireActivity().window)
//            val controller = ViewCompat.getWindowInsetsController(binding.root)
//            controller?.isAppearanceLightStatusBars = true
//        }
        binding.event = this
        binding.recyclerView.adapter = adapter.withLoadStateHeaderAndFooter(
            header = headerAdapter,
            footer = PagerFooterAdapter()
        )
//        binding.recyclerView.addItemDecoration(SpaceItemDecoration(10))
        binding.refreshLayout.setOnRefreshListener {
            adapter.refresh()
        }
        binding.emptyLayout.setOnClickListener {
            adapter.refresh()
        }
    }

    override fun initData() {
        super.initData()

        StoreUtil.getBean<UserInfo>(requireContext())?.let {
            var userId = it.id
            if (userId.isNullOrEmpty()) {
                toast("用户id不能为空")
                return
            }
            viewModel.userId.value = userId
        }

        lifecycleScope.launch {
            viewModel.pagingDataAuto(20, hold = {
                var ret = viewModel.server.get_user_team(viewModel.userId.value!!, it).body()

                ret?.let {
                    withContext(Dispatchers.Main) {
                        headerAdapter.tdBean1 = it.data
                        headerAdapter.notifyDataSetChanged()
                    }
                }

                PagingJsonResult(
                    list = ret?.data?.sub_list,
                    code = ret?.code,
                    message = ret?.message
                )
            }).collect {
                adapter.submitData(it)
            }
        }
        viewModel.userInfo()

        viewModel.isOneLoading.observe(this, {
            if (!it) {
                binding.refreshLayout.isRefreshing = false
            }
        })
    }


    /**
     * 奖励记录
     */
    fun jljl() {
        startActivity(Intent(context, TdJlJlActivity::class.java))
    }

}